package com.ng.application.fragment;

import android.content.Context;
import android.content.Intent;
import android.content.SharedPreferences;
import android.os.Message;
import android.text.TextUtils;
import android.util.Log;
import android.view.View;
import android.widget.Button;
import android.widget.ImageButton;
import android.widget.LinearLayout;
import android.widget.TextView;
import android.widget.Toast;

import com.google.gson.Gson;
import com.ng.application.R;
import com.ng.application.activity.MainActivity;
import com.ng.application.activity.mine.AdviseActivity;
import com.ng.application.activity.mine.LoginActivity;
import com.ng.application.bean.LoginBean;
import com.ng.application.config.Constant;
import com.ng.application.config.Urls;
import com.ng.application.utils.TokenUtils;

import org.json.JSONException;
import org.json.JSONObject;

import java.io.IOException;

import okhttp3.Call;
import okhttp3.Callback;
import okhttp3.FormBody;
import okhttp3.MediaType;
import okhttp3.OkHttpClient;
import okhttp3.Request;
import okhttp3.RequestBody;
import okhttp3.Response;

/**
 * @Author: Ng
 * @Date: 2024/4/9
 * @Versopn: 1.0.0
 * @Description:
 */
public class MineFragment extends BaseFragment implements View.OnClickListener {

    private static final String TAG = MineFragment.class.getSimpleName();

    private ImageButton mine_picture;
    private TextView mine_name;
    private Intent intent = null;
    private LinearLayout mine_info;
    private LinearLayout mine_order;
    private LinearLayout mine_update;
    private LinearLayout mine_advice;
    private Button mine_logout;

    @Override
    public View initView() {
        Log.i(TAG, "用户中心的视图被实例化了");
        View view = View.inflate(getContext(), R.layout.fragment_mine, null);
        mine_picture = view.findViewById(R.id.mine_picture);
        mine_name = view.findViewById(R.id.mine_name);
        mine_advice = view.findViewById(R.id.mine_advice);
        mine_info = view.findViewById(R.id.mine_info);
        mine_order = view.findViewById(R.id.mine_order);
        mine_update = view.findViewById(R.id.mine_update);
        mine_logout = view.findViewById(R.id.mine_logout);

        mine_picture.setOnClickListener(this);
        mine_info.setOnClickListener(this);
        mine_order.setOnClickListener(this);
        mine_update.setOnClickListener(this);
        mine_advice.setOnClickListener(this);
        mine_logout.setOnClickListener(this);
        return view;
    }

    @Override
    public void initData() {
        super.initData();

        // 读取本地保存用户的登录信息
        SharedPreferences sp = getActivity().getSharedPreferences(Constant.APPLICATION_DATA, Context.MODE_PRIVATE);
        String user = sp.getString(Constant.USERNAME, "");
        // 把用户名显示到 textView 控件上
        if (!user.equals("")) {
            mine_name.setText(user);
            mine_logout.setVisibility(View.VISIBLE);
        } else {
            mine_name.setText("点击登录");
            mine_logout.setVisibility(View.INVISIBLE);
        }
    }

    @Override
    public void onClick(View v) {
        switch (v.getId()) {
            case R.id.mine_picture:
                SharedPreferences sp = getActivity().getSharedPreferences(Constant.APPLICATION_DATA, Context.MODE_PRIVATE);
                String user = sp.getString(Constant.USERNAME,"");
                if (!user.equals("")) {
                    intent = new Intent(getContext(), AdviseActivity.class);
                } else {
                    intent = new Intent(getContext(), LoginActivity.class);
                }
                break;
            case R.id.mine_info:
                intent = new Intent(getContext(), AdviseActivity.class);
                break;
            case R.id.mine_order:
                intent = new Intent(getContext(), AdviseActivity.class);
                break;
            case R.id.mine_update:
                intent = new Intent(getContext(), AdviseActivity.class);
                break;
            case R.id.mine_advice:
                intent = new Intent(getContext(), AdviseActivity.class);
                break;
            case R.id.mine_logout:
                logout();
//                SharedPreferences.Editor editor = getActivity().getSharedPreferences(Constant.APPLICATION_DATA, Context.MODE_PRIVATE).edit();
//                editor.clear(); // 清除本地保存的token值
//                editor.apply();
                intent = new Intent(getContext(), LoginActivity.class);
                break;
        }
        // 页面跳转
        startActivity(intent);
    }

    private void logout() {
        SharedPreferences preferences = getActivity().getSharedPreferences(Constant.APPLICATION_DATA, Context.MODE_PRIVATE);
        String token = preferences.getString(Constant.TOKEN, "");

        OkHttpClient client = new OkHttpClient();
        RequestBody requestBody = new FormBody.Builder()
                .build();
        Request request = new Request.Builder()
                .post(requestBody)
                .header("Token", token)
                .url(Urls.USER_LOGOUT)
                .build();
        okhttp3.Call call = client.newCall(request);
        call.enqueue(new Callback() {
            @Override
            public void onFailure(Call call, IOException e) {
                Log.e("【请求状态】", "请求失败！");
            }

            @Override
            public void onResponse(Call call, Response response) throws IOException {
                if (response.isSuccessful()) {
                    Log.i("【响应状态】", "响应成功！");
                    final String loginBody = response.body().string();
                    Gson gson = new Gson();
                    LoginBean loginBean = gson.fromJson(loginBody, LoginBean.class);
                    String code = loginBean.getCode();
                    // 响应成功 判断状态码
                    if (code.equals("200")) {
                        Log.i("【退出状态】", "退出成功！");
                    } else {
                        Log.i("【退出状态】", "退出失败！");
                    }
                }
            }
        });
    }
}
